package controllers

import (
	"encoding/json"
	//	"errors"
	"qysoft/models"
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

// LogController oprations for Log
type LogController struct {
	CommonController
}

// URLMapping 方法映射
func (c *LogController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
	c.Mapping("DeleteLogs", c.DeleteLogs)
}

// Post create Log
// @Title Post
// @Description create Log
// @Param	body		body 	models.Log	true		"body for Log content"
// @Success 201 {int} models.Log
// @Failure 403 body is empty
// @router / [post]
func (c *LogController) Post() {
	var v models.Log
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if _, err := models.AddLog(&v); err == nil {
			c.Ctx.Output.SetStatus(201)
			c.Data["json"] = v
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// GetOne  get Log by id
// @Title Get
// @Description get Log by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Log
// @Failure 403 :id is empty
// @router /:id [get]
func (c *LogController) GetOne() {
	id := c.GetString(":id")
	v, err := models.GetLogById(id)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

// GetAll  get Log
// @Title Get All
// @Description get Log
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Log
// @Failure 403
// @router /GetAll [post]
func (c *LogController) GetAll() {
	//	user := c.user
	rdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	sdata, _ := rdata.(map[string]interface{})
	beego.Debug(sdata)
	psize, _ := sdata["pagesize"].(float64)
	pg, _ := sdata["page"].(float64)
	pagesize := int(psize)
	page := int(pg)
	//	beego.Debug(user)
	sort := sdata["sortby"].([]interface{})
	ord := sdata["order"].([]interface{})
	var ss, oo []string
	for i, e := range sort {
		oo = append(oo, ord[i].(string))
		ss = append(ss, e.(string))
	}
	sortby := ss
	order := oo
	beego.Debug(sortby, order)
	//	var query map[string]string = make(map[string]string)
	limit := pagesize
	offset := (page - 1) * pagesize
	searchKey := strings.TrimSpace(sdata["searchKey"].(string))

	//	l, err := models.GetAllLog(query, fields, sortby, order, offset, limit)
	//	if err != nil {
	//		c.RetError(ErrDatabase)
	//	}
	beego.Debug(searchKey)
	l, count, err := models.GetLogs(sortby, order, offset, limit, searchKey)
	logs := make(map[string]interface{})
	logs["data"] = l
	logs["total"] = count
	beego.Debug(l[0])
	c.RetSuccess(logs)
}

// Delete  delete the Log
// @Title Delete
// @Description delete the Log
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *LogController) Delete() {
	id := c.GetString(":id")
	if err := models.DeleteLog(id); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// DeleteLogs 删除日志列表
//@Title DeleteLogs
// @Description DeleteLogs
// @Success 200  success!
// @Failure 403
// @router /DeleteLogs [post]
func (c *LogController) DeleteLogs() {
	user, _ := c.GetUserinfo()
	rdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	sdata, _ := rdata.([]interface{})
	for _, value := range sdata {
		id, _ := value.(string)
		//		id := int64(eid)
		err := models.DeleteLog(id)
		if err != nil {
			c.RetError(ErrDatabase)
		}
	}
	num := strconv.Itoa(len(sdata))
	c.Dlog("删除了"+num+"条日志", user.Organization.Id)
	rt := make(map[string]interface{})
	rt["Id"] = sdata
	c.RetSuccess(rt)
}
